//
//  userSignUpController.m
//  toolBox
//
//  Created by Alex on 08/07/2011.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "userSignUpController.h"

@implementation userSignUpController
@synthesize usernameField;
@synthesize passwordField;
@synthesize confirmPassword;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    [usernameField setDelegate:self];
    [self.usernameField setReturnKeyType:UIReturnKeyDone];
    [self.usernameField addTarget:self action:@selector(usernameField)forControlEvents:UIControlEventEditingDidEndOnExit];
    [passwordField setDelegate:self];
    [self.passwordField setReturnKeyType:UIReturnKeyDone];
    [self.passwordField addTarget:self action:@selector(passwordField)forControlEvents:UIControlEventEditingDidEndOnExit];
    [confirmPassword setDelegate:self];
    [self.confirmPassword setReturnKeyType:UIReturnKeyDone];
    [self.confirmPassword addTarget:self action:@selector(confirmPassword)forControlEvents:UIControlEventEditingDidEndOnExit];
}


- (void)viewDidUnload
{
    [self setUsernameField:nil];
    [self setPasswordField:nil];
    [self setConfirmPassword:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)submit:(id)sender 
{
    NSLog(@"%@,%@",passwordField.text,confirmPassword.text);
    //NOTE: Add some better validation in the future 
    
    if(![passwordField.text isEqualToString: confirmPassword.text])
    {
        UIAlertView *passwords = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"Passwords do not match" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
        NSLog(@"No match");
        [passwords show];
        return;
        
    }
    userAuthModel *put = [[userAuthModel alloc] createUser:usernameField.text
                                                  password:passwordField.text               
                                                withObject:self
                                              withSelector:@selector(createUserCallback:)];
}
-(void) createUserCallback:(id)sender{
    UIAlertView *success = [[UIAlertView alloc] initWithTitle:@"Congratulations!" message:@"You have successfully created a new user identity" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok"  , nil];
    
    usernameField.text = nil;
    passwordField.text = nil;
    confirmPassword.text = nil;
    [success show];
    
}
@end
